package com.quick.develop.flowable.controller.definition;

import com.quick.develop.framework.common.page.PageResult;
import com.quick.develop.framework.common.page.R;
import com.quick.develop.flowable.domain.definition.BpmProcessDefinitionPageItemRespVO;
import com.quick.develop.flowable.domain.definition.BpmProcessDefinitionPageReqVO;
import com.quick.develop.flowable.domain.definition.BpmProcessDefinitionRespVO;
import com.quick.develop.flowable.service.definition.IBpmProcessDefinitionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author junyuan.liu
 * @description: 
 * @date 2022/4/25 13:56
 */
@Api(tags = "流程管理后台 - 流程定义")
@RestController
@RequestMapping("/process-definition")
@Validated
public class BpmProcessDefinitionController  {
    @Resource
    private IBpmProcessDefinitionService bpmDefinitionService;

    @GetMapping("/page")
    @ApiOperation(value = "获得流程定义分页")
    public R<PageResult<BpmProcessDefinitionPageItemRespVO>> getProcessDefinitionPage(
            BpmProcessDefinitionPageReqVO pageReqVO){
        return R.ok(bpmDefinitionService.getProcessDefinitionPage(pageReqVO));
    }


    @GetMapping("/start-page")
    @ApiOperation(value = "发起流程定义分页")
    public R<PageResult<BpmProcessDefinitionRespVO>> getProDefPage(BpmProcessDefinitionPageReqVO pageVo){
        return R.ok(bpmDefinitionService.getProDefPage(pageVo));
    }

    @GetMapping ("/getBpmnXmlByDeploymentId")
    @ApiOperation(value = "获得流程定义的 BPMN XML 通过部署ID")
    public R<String> getBpmnXmlByDeploymentId(@RequestParam("id") String id) throws Exception{
        return R.ok(bpmDefinitionService.getBpmnXmlByDeploymentId(id));
    }

    @GetMapping ("/getBpmnXmlByDefinitionId")
    @ApiOperation(value = "获得流程定义的 BPMN XML 通过定义ID")
    public R<?> getBpmnXmlByDefinitionId(@RequestParam("id") String id) throws Exception{
        return R.ok(bpmDefinitionService.getBpmnXmlByDefinitionId(id));
    }

    @GetMapping ("/getActiveProcessDefinitionByKey")
    @ApiOperation(value = "通过key获取最新的定义版本")
    public R<?> getActiveProcessDefinitionByKey(@RequestParam("key") String key){
        return R.ok(bpmDefinitionService.getActiveProDefByKey(key));
    }

    @PostMapping("/clear-deploy-cache/{definitionId}")
    @ApiOperation(value = "清除流程模型")
    public R<?> clearDeploymentCache(@PathVariable("definitionId") String definitionId) {
        bpmDefinitionService.clearDeploymentCache(definitionId);
        return R.ok();
    }


}
